package com.bss.service;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.andy.basics.Dao;
import com.bss.dao.AdmissionDao;
import com.bss.dao.MaterialAllocationDao;
import com.bss.dao.PatientDao;
import com.bss.dao.PaymentDao;
import com.bss.dao.PaymentTypeDao;
import com.bss.model.Admission;
import com.bss.model.MaterialAllocation;
import com.bss.model.Payment;

@Service
@Scope("session")
public class UpdateAdmissionService extends com.andy.basics.Service<Admission> {

	private static final Logger LOGGER = Logger.getLogger(UpdateAdmissionService.class.getName());

	@Autowired
	private AdmissionDao admissionDao;

	@Autowired
	private MaterialAllocationDao materialAllocationDao;

	@Autowired
	private PatientDao patientDao;

	@Autowired
	private PatientService patientService;

	private int utensilDeposit;

	@Autowired
	private PaymentTypeDao paymentTypeDao;

	@Autowired
	private PaymentDao paymentDao;

	@Transactional
	public void update() {
		Admission old = this.admissionDao.findById(this.getModel().getId());
		this.getModel().getPatient().setDate(new Date());
		if (this.getModel().getPatient().getPhoto() == null) {
			this.getModel().getPatient().setPhoto(this.patientService.getImageData());
		}
		this.patientDao.update(this.getModel().getPatient());
		this.getModel().setDate(new Date());

		for (MaterialAllocation ma : this.getModel().getMaterialAllocations()) {
			this.materialAllocationDao.update(ma);
		}

		UpdateAdmissionService.LOGGER.debug("Utensil deposit: " + this.utensilDeposit + ", OLD " + old.getMaterialSet());

		if ((this.utensilDeposit != 0)
				&& ((old.getMaterialSet() == null) || ((this.getModel().getMaterialSet() != null)
						&& (this.getModel().getMaterialSet().getId() != null) && !this.getModel().getMaterialSet().getId()
						.equals(old.getMaterialSet().getId())))) {
			Payment payment = new Payment();
			payment.setAmount(this.utensilDeposit);
			payment.setPaymentType(this.paymentTypeDao.find("name='Utensil Deposit'").get(0));
			payment.setPayable(this.getModel());
			payment.setReceipt(null);
			this.paymentDao.persist(payment);
		}

		this.admissionDao.update(this.getModel());
	}

	@Override
	public Dao<Admission> getDao() {
		return this.admissionDao;
	}

	public void cancel() {
		this.reload();
	}

	@Transactional
	public void selectAdmission(Admission admission) {
		this.setModel(this.admissionDao.findById(admission.getId()));
		this.getModel().getMaterialAllocations().size();
		this.getModel().getPayments().size();

		this.utensilDeposit = 0;

		for (Payment payment : this.getModel().getPayments()) {
			if (!payment.isRefund() && payment.getPaymentType().getName().equals("Utensil Deposit")) {
				this.utensilDeposit += payment.getAmount();
			}
		}
	}

	public int getUtensilDeposit() {
		return this.utensilDeposit;
	}

	public void setUtensilDeposit(int utensilDeposit) {
		this.utensilDeposit = utensilDeposit;
	}

}
